import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import java.io.File;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.*;
import java.lang.ArrayIndexOutOfBoundsException;
public class Admin_page extends JPanel {

	public JFrame admin_add_user;
	public JPanel panel;
	public JLabel user_label;
	public JTextField user_name;
	public JLabel pw_label;
	public JTextField pw;
	public JLabel first_name_label ;
	public JTextField first_name ;
	public JLabel last_name_label ;
	public JTextField last_name ;
	public Add_Courses_Frame course_Frame;
	public int [] courses;
	String firstname;
	String lastname;
	String username;
	String password;
	// user and user []
	User user = new User();
	
    public Admin_page() 
	{
        admin_add_user = new JFrame("Add user");
        admin_add_user.setSize(500, 100);
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        panel = new JPanel();
		
        user_label = new JLabel("User Name");
        user_name = new JTextField(15);
        pw_label = new JLabel("Password");
        pw = new JTextField(15);
		
        first_name_label = new JLabel("First Name");
        first_name = new JTextField(15);
        last_name_label = new JLabel("Last Name");
        last_name = new JTextField(15);
		
        JButton add_user = new JButton("Add courses for user");
        JButton add_from_file =  new JButton("Add users from file");
        JButton define_user = new JButton("Add user and courses");
        JTextField Dept = new JTextField(15);
        JLabel Dept_label = new JLabel("Dept");
        JTextField Course_Number = new JTextField(15);
        JLabel Course_Number_label = new JLabel("Course Number");
        JTextField Section = new JTextField(15);
        JLabel Section_label = new JLabel("Section");
        JTextField Class_Number = new JTextField(15);
        JLabel Class_Number_Label = new JLabel("Class Number");

        add(add_from_file);
		add_user.addActionListener(new ButtonListener());
		add_from_file.addActionListener(new ButtonListener());
		define_user.addActionListener(new ButtonListener());
		add(user_label);
		add(user_name);
		add(first_name_label);
		add(first_name);
		add(last_name_label);
		add(last_name);
		add(pw_label);
		add(pw);
		add(add_user);
		add(define_user);
    }//end admin_page constructor

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new Admin_page());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 350);
    frame.setVisible(true);
  }


	class ButtonListener implements ActionListener {
		void getUsers(String filename){

			System.out.println("in getUsers method");
			File inFile;
			String filepath = new String();
			User tempuser = new User();

			Scanner scanner = new Scanner(System.in);
			Scanner sc = new Scanner("");
			String line;
			String [] userline = new String [10];
			User [] users;// = new User[3];
			String firstname;
			String lastname;
			String password;
			String testfirstnames = new String();
			//courses= new int[10];
			int numofcourses = 0;
			int i;
			int j = 0;
			int length = userline.length;
			
			//code to count number of lines (users)
			LineNumberReader lineCounter = null;
			try {
				lineCounter = new LineNumberReader(
				new InputStreamReader(new FileInputStream(filename)));
				String nextLine = null;
			
				while ((nextLine = lineCounter.readLine()) != null) {
					if (nextLine == null)
						break;
					System.out.println(nextLine);
				}
				System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
				
			} catch (Exception done) {
				done.printStackTrace();
			} 
			//
			users = new User[lineCounter.getLineNumber()];
			
			try  {
				System.out.println("in try method");
				System.out.println("help " +filename);
				inFile = new File(filename);
				filepath = inFile.getAbsolutePath();
				System.out.println("filepath" +filepath);
				sc = new Scanner(inFile);
				System.out.println("in try method");
				int numberofusers = 0;
		  
				while(sc.hasNextLine()){
					System.out.println("sc has next line");
					line = sc.nextLine();
					System.out.println(line);
					userline = line.split(";");

					for( i = 0; i<length;i++){
						try{

						System.out.println(userline[i]);
						} catch(ArrayIndexOutOfBoundsException e){
						 j++;

						}

					}
					
					numofcourses = 10 - j - 4;
					System.out.println("Numofcourses = "  +numofcourses);
					courses = null;
					courses = new int [numofcourses];
					for(int q = 0; q<numofcourses; q++)
					{
						courses[q] = Integer.parseInt(userline[q+4]);
						System.out.println(courses[q]);
					}
					System.out.println("Number of classes = " +numofcourses);
					//System.out.println("Number of classes = " +i + " " +j);
					j = 0;
					System.out.println("Trying to create user " +numberofusers +" first " +userline[0] +" last " + userline[1] +" username " +userline[2] +" password " +userline[3]);
					//tempuser.DefineUser(userline[0],userline[1],userline[2],userline[3],courses);
					tempuser = new User();
					users[numberofusers] = tempuser;
					users[numberofusers].DefineUser(userline[0],userline[1],userline[2],userline[3],courses);
					numberofusers++;

				}
				SystemController scon = GUIMaster.getWorkhorse();
		    scon.addMultipleUsers(users);
		/*  for(int w =0; w<numberofusers;w++)
		  {
			  testfirstnames = users[w].getUserFirst();
			  System.out.println(testfirstnames);
		  }
			 //for(int i = 0; i <line){
		*/
			 // System.out.println(line);
		  //}

			} catch (FileNotFoundException e){
			  System.out.println("in Exception method");
			}
			scanner.close();
			sc.close();

		}//end get users

		ButtonListener() {
		}

	  
		public void actionPerformed(ActionEvent e) {
			GUIMaster gm = GUIMaster.getInstance();
			SystemController sc = GUIMaster.getWorkhorse();
			if (e.getActionCommand().equals("Add courses for user")) {
				course_Frame = new Add_Courses_Frame();

			}
			else if(e.getActionCommand().equals("Add user and courses"))
			{
			   System.out.println(course_Frame.get_numofcourses());
			   int numofcourses = course_Frame.get_numofcourses();
			   courses = new int[numofcourses];
			   courses = course_Frame.get_courses();
			   firstname = first_name.getText();
			   lastname = last_name.getText();
			   username = user_name.getText();
			   password = pw.getText();
			   // System.out.println("Trying to create user " +" first " + firstname+" last " +lastname  +" username " + username+" password " +password);
			   user.DefineUser(firstname, lastname, username, password, courses);
			   sc.addUser(user);
			   
			   
			}
			else if(e.getActionCommand().equals("Add users from file"))
			{   
				JFileChooser chooser = new JFileChooser();
				chooser.setCurrentDirectory(new File("."));

				chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
					public boolean accept(File f) {
						return f.getName().toLowerCase().endsWith(".txt")
							|| f.isDirectory();
					}

					public String getDescription() {
						return "GIF Images";
					}
				});

				int r = chooser.showOpenDialog(new JFrame());
				if (r == JFileChooser.APPROVE_OPTION) {
					String name = chooser.getSelectedFile().getName();
					//System.out.println(name);
					getUsers(name);
				}
			}
		}//end action performed
	}//end button listener class
}//end admin_page class
